export default {
  // v-debounce:time="function"
  install(Vue) {
    Vue.directive("debounce", {
      // 当被绑定的元素插入到 DOM 中时……
      inserted: (el, binding) => {
        if (typeof binding.value !== "function") {
          throw `v-${binding.name}="callback" -> callback must be a function`;
        }
        if (binding.arg && isNaN(Number(binding.arg))) {
          throw `v-${binding.name}:time -> time must be a Number`;
        }
        const distance = binding.arg ? Number(binding.arg) : 500;
        let timer;
        el.addEventListener("click", () => {
          if (timer) {
            clearTimeout(timer);
          }
          timer = setTimeout(() => {
            binding.value();
          }, distance);
        });
      },
    });
  },
};
